package cn.didi.feidi.androidorder.fragment.personal;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.activity.personal.PersonBalanceDetailActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonRechargeActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonWalletPaySettingActivity;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.activity.personal.PersonToCashActivity;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;

/**
 * A simple {@link Fragment} subclass.
 */
public class PersonWalletFragment extends FeidiBaseFragment {

    private TextView walletBalance;

    private LinearLayout recharge;
    private LinearLayout toCash;
    private LinearLayout balanceDetail;
    private LinearLayout paySetting;

    private static final String TAG = "PersonWalletFragment";

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonWalletFragment newInstance(Bundle bundle) {
        PersonWalletFragment fragment = new PersonWalletFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_wallet, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }

    @Override
    protected void initViews(View rootView) {
        walletBalance = (TextView) rootView.findViewById(R.id.person_wallet_banlance_tv);

        recharge = (LinearLayout) rootView.findViewById(R.id.person_wallet_recharge);
        toCash = (LinearLayout) rootView.findViewById(R.id.person_wallet_to_cash);
        balanceDetail = (LinearLayout) rootView.findViewById(R.id.person_wallet_balance_detail);
        paySetting = (LinearLayout) rootView.findViewById(R.id.person_wallet_pay_setting);
    }

    @Override
    protected void initData() {
        walletBalance.setText(String.valueOf(UserManager.getMoney()));
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();

        params.put("userId", UserManager.getUserId());
        params.put("token", UserManager.getToken());

        executeRequest(new AppActionRequest<Double>(Api.GET_MONEY, params,
                new TypeToken<ApiResponse<Double>>() {
                }.getType(), new ActionCallbackListener<Double>() {
            @Override
            public void onSuccess(String message, Double data) {
                if (data != null) {
                    walletBalance.setText(String.valueOf(data));
                    UserManager.setMoney(data);
                } else {
                    makeToast("success" + message);
                    Log.d(TAG, message);
                }
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);

                switch (errorEvent) {
                    default:
                        makeToast("未知错误");
                        break;
                }
            }
        }));
    }

    @Override
    protected void addListener() {
        recharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PersonRechargeActivity.class);
                startActivity(intent);
            }
        });

        toCash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PersonToCashActivity.class);
                startActivity(intent);
            }
        });

        balanceDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PersonBalanceDetailActivity.class);
                startActivity(intent);
            }
        });

        paySetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PersonWalletPaySettingActivity.class);
                startActivity(intent);
            }
        });
    }


}
